package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.PageResult;
import cn.outsourcing.supermarket.common.utils.IPageUtils;
import cn.outsourcing.supermarket.common.utils.PageResultUtils;
import cn.outsourcing.supermarket.common.utils.StrUtils;
import cn.outsourcing.supermarket.converter.HiolabsCartConverter;
import cn.outsourcing.supermarket.converter.HiolabsGoodsConverter;
import cn.outsourcing.supermarket.converter.HiolabsUserConverter;
import cn.outsourcing.supermarket.dto.OnlyIdPayloadDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageOnlyIdPageDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageUserListDTO;
import cn.outsourcing.supermarket.mapper.*;
import cn.outsourcing.supermarket.po.HiolabsCart;
import cn.outsourcing.supermarket.po.HiolabsFootprint;
import cn.outsourcing.supermarket.po.HiolabsUser;
import cn.outsourcing.supermarket.service.BackstageUserService;
import cn.outsourcing.supermarket.vo.backstage.BackstageUserDataInfoVO;
import cn.outsourcing.supermarket.vo.backstage.BackstageUserListVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsCartVO;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsGoodsVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author gzkemays
 * @since 2021/11/4 18:17
 */
@Service
public class BackstageUserServiceImpl implements BackstageUserService {
  @Resource HiolabsUserMapper userMapper;
  @Resource BackstageUserMapper backstageUserMapper;
  @Resource HiolabsCartMapper cartMapper;
  @Resource HiolabsGoodsMapper goodsMapper;

  @Resource HiolabsFootprintMapper footprintMapper;

  @Override
  public Integer updateInfo(BackstageUserListDTO dto) {
    HiolabsUser user = new HiolabsUser();
    user.setId(dto.getId());
    user.setNickname(StrUtils.getBase64Str(dto.getNickname()));
    return userMapper.updateById(user);
  }

  @Override
  public BackstageUserListVO getUserInfo(OnlyIdPayloadDTO dto) {
    return HiolabsUserConverter.INSTANCE.convertToBackstageUserVo(
        userMapper.selectById(dto.getId()));
  }

  @Override
  public BackstageUserDataInfoVO getUserDataInfo(OnlyIdPayloadDTO dto) {
    return backstageUserMapper.getComprehensiveData(dto.getId());
  }

  @Override
  public PageResult<HiolabsCartVO> getCartShop(BackstageOnlyIdPageDTO dto) {
    IPageUtils<HiolabsCart> properties = IPageUtils.<HiolabsCart>init().properties(dto);
    List<HiolabsCart> records =
        cartMapper
            .selectPage(
                properties,
                Wrappers.<HiolabsCart>lambdaQuery()
                    .eq(HiolabsCart::getUserId, dto.getId())
                    .orderByDesc(HiolabsCart::getAddTime))
            .getRecords();
    return PageResultUtils.parseListPageObjs(
        properties, HiolabsCartConverter.INSTANCE.convertToVoList(records));
  }

  @Override
  public PageResult<HiolabsGoodsVO> getFootPrint(BackstageOnlyIdPageDTO dto) {
    IPageUtils<HiolabsFootprint> properties = IPageUtils.<HiolabsFootprint>init().properties(dto);
    Set<Integer> footPrintGoodsIds =
        footprintMapper
            .selectPage(
                properties,
                Wrappers.<HiolabsFootprint>lambdaQuery()
                    .eq(HiolabsFootprint::getUserId, dto.getId())
                    .orderByDesc(HiolabsFootprint::getAddTime))
            .getRecords()
            .stream()
            .map(HiolabsFootprint::getGoodsId)
            .collect(Collectors.toSet());
    return PageResultUtils.parseListPageObjs(
        properties,
        HiolabsGoodsConverter.INSTANCE.convertToVoList(
            goodsMapper.selectBatchIds(footPrintGoodsIds)));
  }
}
